import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
//import org.apache.commons.codec.binary.Base64;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by guoff on 2018-12-26.
 */
public class AES
{
    private static String rawKey = "7a3fd7c9bae0390541715f17f2f528fe";
    private static String heiKey = "7a38b7c9bae03905db379297f24948fe";
    private static String datasourceKey = "8efdc86723de7fef31da6bccbac089b9";
    private static final String algorithm = "AES";

    AES(String rawKeynew,String heiKeynew,String datasourceKeynew){        //定义构造函数，输出Hello World
        System.out.println("Hellow World");
        rawKey = rawKeynew;
        heiKey = heiKeynew;
        datasourceKey = datasourceKeynew;

    }

    private static String asHex(byte[] buf)
    {
        StringBuffer strbuf = new StringBuffer(buf.length * 2);

        for (int i = 0; i < buf.length; i++) {
            if ((buf[i] & 0xFF) < 16) {
                strbuf.append("0");
            }
            strbuf.append(Long.toString(buf[i] & 0xFF, 16));
        }
        return strbuf.toString();
    }

    private static byte[] asBin(String src)
    {
        if (src.length() < 1)
            return null;
        byte[] encrypted = new byte[src.length() / 2];
        for (int i = 0; i < src.length() / 2; i++) {
            int high = Integer.parseInt(src.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(src.substring(i * 2 + 1, i * 2 + 2), 16);

            encrypted[i] = ((byte)(high * 16 + low));
        }
        return encrypted;
    }

    private static String getRawKey()
    {
        try
        {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128);

            SecretKey skey = kgen.generateKey();
            byte[] raw = skey.getEncoded();
            return asHex(raw); } catch (Exception e) {
        }
        return "";
    }

    public static String setCarNo(String message)
    {
        return getEncrypt(message, rawKey);
    }

    public static String getCarNo(String message)
    {
//        System.out.println("rawKey"+rawKey);
        return getDecrypt(message, rawKey);
    }

    public static String set_lotoCarNo(String message) {
        return getDecrypt(message, heiKey);
    }

    public static String get_lotoCarNo(String message) {
        return getEncrypt(message, heiKey);
    }
    public static String get_datasourceAes(String message) {
        return getDecrypt(message, datasourceKey);
    }
    private static String set_datasourceAes(String message) {
        return getEncrypt(message, datasourceKey);
    }

    public static String getEncrypt(String message, String rawKey)
    {
        byte[] key = asBin(rawKey);
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");

            Cipher cipher = Cipher.getInstance("AES");

            cipher.init(1, skeySpec);

            byte[] encrypted = cipher.doFinal(message.getBytes());

            return asHex(encrypted); } catch (Exception e) {
        }
        return "";
    }

    public static String getDecrypt(String encrypted, String rawKey)
    {
        byte[] tmp = asBin(encrypted);
        byte[] key = asBin(rawKey);
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(2, skeySpec);
            byte[] decrypted = cipher.doFinal(tmp);
            return new String(decrypted); } catch (Exception e) {
        }
        return "";
    }

    public static String decodeStr(String str)
    {
        byte[] debytes = Base64.decodeBase64(new String(str).getBytes());
        return new String(debytes);
    }


    public static void main(String[] args) {
        System.out.println(set_datasourceAes("wtp"));

        System.out.println(setCarNo("2"));
        System.out.println(getCarNo("38580c33e758be5389c5e6cb27749669edc2c4fb8f060e5970a9331c5c12d397"));

        System.out.println("          xxx" + Base64.encodeBase64String(setCarNo("123").getBytes()));

        System.out.println(decodeStr(getCarNo("47f9eb1c059c9404a9d39faaaaac15bbedc2c4fb8f060e5970a9331c5c12d397")));
        System.out.println("解密3             " + setCarNo(decodeStr(getCarNo("0dc4ad665e77b7fba6e3e14d106bf666"))));
        System.out.println("解密4             " + setCarNo(new StringBuilder(String.valueOf(decodeStr(getCarNo("5b1219eb22d4f8ecc71de4e27ece5d01")))).append(decodeStr(getCarNo("9471ce458e6adde842f3aa7cc0c4ed0b"))).toString()));

        System.out.println("xxx");
        System.out.println(set_lotoCarNo("f9571b377d29eb1be15faa81917c62686d60052a032eedecfe36bfe17548f04f"));
        System.out.println("解密5：" + set_lotoCarNo("f5dff64ee303a2d1fbd8984d61b08f396d60052a032eedecfe36bfe17548f04f"));

        /*test*/
        System.out.println("解密99：" +setCarNo(decodeStr(getCarNo("d8fd6537d5c24081c4fa0172146ca0fe"))));
        System.out.println(getCarNo("d8fd6537d5c24081c4fa0172146ca0fe"));
        System.out.println(getCarNo("78cad2f4a3a658e73b01352059b7be5bedc2c4fb8f060e5970a9331c5c12d397"));
        System.out.println(getCarNo("4d6df1b2ecb2fa66f14bafc5fa7032f9"));

    }
}